package com.atguigu.lease.web.app.service.impl;

import com.atguigu.lease.common.context.LoginUserContext;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.model.enums.LeaseStatus;
import com.atguigu.lease.web.app.mapper.*;
import com.atguigu.lease.web.app.service.ApartmentInfoService;
import com.atguigu.lease.web.app.service.BrowsingHistoryService;
import com.atguigu.lease.web.app.service.RoomInfoService;
import com.atguigu.lease.web.app.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.app.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.app.vo.attr.AttrValueVo;
import com.atguigu.lease.web.app.vo.fee.FeeValueVo;
import com.atguigu.lease.web.app.vo.graph.GraphVo;
import com.atguigu.lease.web.app.vo.room.RoomDetailVo;
import com.atguigu.lease.web.app.vo.room.RoomItemVo;
import com.atguigu.lease.web.app.vo.room.RoomQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@Service
@Slf4j
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {
    @Autowired
    private RoomInfoMapper roomInfoMapper;

    @Autowired
    ApartmentInfoMapper apartmentInfoMapper;

    @Autowired
    private GraphInfoMapper graphInfoMapper;

    @Autowired
    private RoomAttrValueMapper roomAttrValueMapper;

    @Autowired
    private FacilityInfoMapper FacilityInfoMapper;

    @Autowired
    private LabelInfoMapper labelInfoMapper;

    @Autowired
    private ApartmentFeeValueMapper apartmentFeeValueMapper;

    @Autowired
    private LeaseTermMapper leaseTermMapper;

    @Autowired
    private BrowsingHistoryService browsingHistoryService;


    @Override
    public IPage<RoomItemVo> roomInfoPageItem(IPage<RoomItemVo> page, RoomQueryVo queryVo) {
        return roomInfoMapper.roomInfoPageItem(page, queryVo);
    }

    @Override
    public RoomDetailVo getDetailById(Long id , String token) {
        RoomDetailVo roomDetailVo = new RoomDetailVo();
        RoomInfo roomInfo = roomInfoMapper.selectById(id);
        BeanUtils.copyProperties(roomInfo, roomDetailVo);

        ApartmentItemVo apartmentItemVo = new ApartmentItemVo();
        ApartmentInfo apartmentInfo = apartmentInfoMapper.getDetailById(roomInfo.getApartmentId());
        BeanUtils.copyProperties(apartmentInfo, apartmentItemVo);
        apartmentItemVo.setGraphVoList(graphInfoMapper.selectApartmentGraphInfo(roomInfo.getApartmentId()));
        apartmentItemVo.setLabelInfoList(labelInfoMapper.selectLabelInfo(roomInfo.getApartmentId()));
        apartmentItemVo.setMinRent(apartmentInfoMapper.selectMinRent(roomInfo.getApartmentId()));
        roomDetailVo.setApartmentItemVo(apartmentItemVo);

        List<GraphVo> graphVoList = graphInfoMapper.selectRoomGraphListById(id);
        roomDetailVo.setGraphVoList(graphVoList);

        List<AttrValueVo> attrValueVoList = roomAttrValueMapper.selectAttrValueListById(id);
        roomDetailVo.setAttrValueVoList(attrValueVoList);

        List<FacilityInfo> facilityInfoList = FacilityInfoMapper.selectRoomFacilityInfoListById(id);
        roomDetailVo.setFacilityInfoList(facilityInfoList);

        List<LabelInfo> labelInfoList = labelInfoMapper.selectRoomLabelInfoListById(id);
        roomDetailVo.setLabelInfoList(labelInfoList);

        List<FeeValueVo> feeValueVoList = apartmentFeeValueMapper.selectReeValueListById(roomInfo.getApartmentId());
        roomDetailVo.setFeeValueVoList(feeValueVoList);

        List<LeaseTerm> leaseTermList = leaseTermMapper.selectLeaseTermList();
        roomDetailVo.setLeaseTermList(leaseTermList);

        List<PaymentType> paymentTypeList = roomInfoMapper.selectPaymentTypeList();
        roomDetailVo.setPaymentTypeList(paymentTypeList);

        roomDetailVo.setIsDelete(roomInfo.getIsDeleted() == 1);

        roomDetailVo.setIsCheckIn(roomInfoMapper.getIsCheckIn(id));

        browsingHistoryService.saveHistory(LoginUserContext.getLoginUserId(token), id);

        return roomDetailVo;


    }

    @Override
    public IPage<RoomItemVo> roomInfoPageItemByApartmentId(IPage<RoomItemVo> page, Long id) {
        return roomInfoMapper.roomInfoPageItemByApartmentId(page, id);
    }
}




